<template>
  <div class="login-right">
    <div class="register">
      <router-link to="/login/forgetPwd">
        <svg-icon icon-class="back" />
      </router-link>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
      <div :class="[isRight? 'error-msg transparent': 'error-msg']">
        两次密码输入不一致，请重新输入
      </div>
      <h3 class="title">重置密码</h3>
      <p class="prompt">请填写您的密码及确认密码！</p>
      <el-form-item :class="[isRight? '': 'error-border']">
        <el-input v-model="loginForm.password" name="password" type="password" placeholder="请输入密码"/>
      </el-form-item>
      <el-form-item :class="[isRight? '': 'error-border']">
        <el-input
          v-model="loginForm.confirmPwd"
          type="password"
          placeholder="请再次确认密码"
          @keyup.enter.native="handleConfirm"/>
      </el-form-item>
      <el-form-item class="btn">
        <el-button :loading="loading" type="primary" style="width:100%;" @click="handleConfirm">
          确定
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { IsEquallyPassword } from '@/utils/validate'
import { ResetPwd } from '@/api/login'
import md5 from 'md5'

export default {
  name: 'ForgetPwd',
  data() {
    return {
      loginForm: {
        password: '',
        confirmPwd: ''
      },
      loginRules: {
        confirmPwd: [{ required: true, trigger: 'blur', validator: IsEquallyPassword }]
      },
      isRight: true,
      loading: false
    }
  },
  methods: {
    // 进行重置密码
    handleConfirm() {
      if (this.loginForm.password !== this.loginForm.confirmPwd) {
        this.isRight = false
      } else {
        this.isRight = true
        const account = this.$store.getters.reset_account
        const password = md5(this.loginForm.password)
        const repassword = md5(this.loginForm.confirmPwd)
        ResetPwd(account, password, repassword).then(response => {
          if (response.status === 200) {
            this.$router.push({ path: '/login' })
          } else {
            console.log('重置失败')
          }
        })
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  $pink: #Faa39e;
  $light_orange: rgba(254, 241, 240, 1);
  $red: rgba(244, 41, 53, 1);
  .error-msg {
    width: 320px;
    height: 28px;
    background: $light_orange;
    border-radius: 2px;
    border: 1px solid $pink;
    text-align: center;
    line-height: 28px;
    font-size: 12px;
    font-family: MicrosoftYaHei;
    color: $red;
  }

  .transparent {
    opacity: 0;
  }

  .error-border {
    border: 1px solid $pink;
  }

  .btn {
    margin-top: 44px;
  }
</style>

